#include "MouseMap.h"
#include <iostream>
#include <sfml/System.hpp>
using namespace std;

MouseMap::~MouseMap(void)
{
}

MouseMapDirection MouseMap::Lookup(int row, int col)
{
	return mmdLookUp.at(col*size.x+row);
}
//POS.x = ROW, POS.y = COL .. so the order of the params is pos.x(row), pos.y(col)
MouseMapDirection MouseMap::Lookup(sf::Vector2i pos)
{
	return mmdLookUp.at(pos.y*size.x+pos.x);
}

MouseMap::MouseMap(sf::Image* img)
{
	Load(img);
}
bool comp(const sf::Color& first, const sf::Color& second)
{
	return ((first.r == second.r ) && (first.g == second.g) && (first.b == second.b));
}

void MouseMap::Load(sf::Image* img)
{
	if(img) {
		int r(0),black(0),b(0),white(0), other(0);
		mmdLookUp.resize(img->GetWidth()*img->GetHeight());
		int rows = img->GetWidth();

		size.x = img->GetWidth();
		size.y = img->GetHeight();

		for(int i=0; i<(int)img->GetHeight(); i++) {
			for(int j=0; j<(int)img->GetWidth(); j++) {
				int ind = i*rows + j;
				if(comp(img->GetPixel(j,i),sf::Color::White)) {
					mmdLookUp[ind] = MM_SW;
					white++;
				} else if(comp(img->GetPixel(j,i),sf::Color::Black)) {
					mmdLookUp[ind] = MM_SE;
					black++;
				} else if(comp(img->GetPixel(j,i), sf::Color::Red)) {
					mmdLookUp[ind] = MM_NW;
					r++;
				} else if(comp(img->GetPixel(j,i), sf::Color::Yellow)) {
					mmdLookUp[ind] = MM_NE;
					b++;
				} else {
					mmdLookUp[ind] = MM_CENTER;
					other++;

				}
			}
		}
		std::cout << "Red: " << r << " | Blue: " << b << " | Black: " 
			<< black << " | White: " << white << " | Other: " 
			<< other << std::endl;
	} else {
		//print error or something
	}

}
